﻿@namespace LaserMark.Components.Components
@using LaserMark.Components.Services
@using LaserMark.DataAccess.Service
@using LaserMark.View.Services
<div class="action-log">
    <div class="row g-3">
        <div class="col-12">
            <Segmented @bind-Value="value" OnValueChanged="@OnValueChanged" Items="@SegmentedOptions">
            </Segmented>
        </div>
        <div class="col-12">
            <Timeline Items="@CustomerTimelineItems" />
        </div>
    </div>
</div>


@code {
    [NotNull]
    private List<TimelineItem>? CustomerTimelineItems { get; set; }

    [Inject]
    [NotNull]
    private IAuditService? AuditService { get; set; }

    [Inject]
    [NotNull]

    private IGlobalStatus? GlobalStatus { get; set; }

    [NotNull]
    private List<SegmentedOption<string>>? SegmentedOptions { get; set; } = new List<SegmentedOption<string>>()
    {
        new SegmentedOption<string>(){ Active = true, Text = "本次", Value = "Once" },
        new SegmentedOption<string>(){ Active = true, Text = "本周", Value = "Week" },
        new SegmentedOption<string>(){ Active = true, Text = "本月", Value = "Month" }
    };

    [NotNull]
    private string? value { get; set; }


    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        CustomerTimelineItems = GlobalStatus.AuditLog.OrderByDescending(s => s.CreateTime).Select(s => new TimelineItem() { Content = s.Description, Description = s.CreateTime.ToString("yyyy-MM-dd ss:HH:mm"), Color = GetColor(s.Result) }).ToList();
    }

    private async Task OnValueChanged(string value)
    {

        if (value == "Once")
        {
            CustomerTimelineItems.Clear();
            CustomerTimelineItems = GlobalStatus.AuditLog.OrderByDescending(s => s.CreateTime).Select(s => new TimelineItem() { Content = s.Description, Description = s.CreateTime.ToString("yyyy-MM-dd ss:HH:mm"), Color = GetColor(s.Result) }).ToList();
            StateHasChanged();
        }
        else if (value == "Week")
        {
            CustomerTimelineItems.Clear();
            CustomerTimelineItems = (await AuditService.GetAll()).Where(s => s.CreateTime > DateTime.Now.AddDays(-1).AddSeconds(1) && s.CreateTime < DateTime.Now.AddDays(7).AddSeconds(-1)).OrderByDescending(s => s.CreateTime).Select(s => new TimelineItem() { Content = s.Description, Description = s.CreateTime.ToString("yyyy-MM-dd ss:HH:mm"), Color = GetColor(s.Result) }).ToList();
            StateHasChanged();
        }
        else if (value == "Month")
        {
            CustomerTimelineItems.Clear();
            CustomerTimelineItems = (await AuditService.GetAll()).Where(s => s.CreateTime > DateTime.Now.AddDays(-1).AddSeconds(1) && s.CreateTime < DateTime.Now.AddDays(30).AddSeconds(-1)).OrderByDescending(s => s.CreateTime).Select(s => new TimelineItem() { Content = s.Description, Description = s.CreateTime.ToString("yyyy-MM-dd ss:HH:mm"), Color = GetColor(s.Result) }).ToList();
            StateHasChanged();
        }
    }

    private Color GetColor(Error error) => error switch
    {
        Error.Success => Color.Success,
        Error.Waring => Color.Warning,
        Error.Info => Color.Info,
        Error.Error => Color.Danger,
        _ => Color.Secondary
    };
}
